package com.imti.fragmentdemo;

import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.lifecycle.Observer;

public class DynamicFragment extends Fragment
{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        MyViewModel.getInstance().getStr(this, new Observer<String>()
        {
            @Override
            public void onChanged(String s)
            {
                Toast.makeText(requireActivity(), s,
                    Toast.LENGTH_SHORT).show();
            }
        });

        View rootView =
            inflater.inflate(R.layout.fragment_dynamic, container, false);

        initView(rootView);

        return rootView;
    }

    private void initView(View root)
    {
        root.findViewById(R.id.text_dynamic_hello);

        //requireActivity()尽量不要调用requireContext()
    }
}